package com.xiaochen.sjzremind.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiaochen.sjzremind.common.Response;
import com.xiaochen.sjzremind.service.IUserService;
import com.xiaochen.sjzremind.service.RedissonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xc
 * @date 2025/10/09/18:19
 * @desc 用户token控制类
 */
@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/user")
public class userController implements IUserService {
    
    @Resource
    private RedissonService redissonService;
    
    @RequestMapping(value = "/set",method = RequestMethod.GET)
    @Override
    public Response<String> saveUserToken(String token,String pushSite) {
        // 不验证签名的解析方式
        String[] chunks = token.split("\\.");
        Base64.Decoder decoder = Base64.getUrlDecoder();
        String payload = new String(decoder.decode(chunks[1]));
        log.info("payload:{}", payload);
//        // 手动解析payload为Claims
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> claimsMap;
        try {
            claimsMap = objectMapper.readValue(payload, new TypeReference<Map<String, Object>>() {
                @Override
                public Type getType() {
                    return super.getType();
                }
            });
            
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        
        Integer exp = (Integer) claimsMap.get("exp");
        //获取当前时间戳
        Long now = System.currentTimeMillis() / 1000;
        Integer id = (Integer) claimsMap.get("id");
        redissonService.setValue("user:" + String.valueOf(id),token, (exp - now) * 1000);
        redissonService.setValue("user:" + String.valueOf(id) + ":pushSite",pushSite, (exp - now) * 1000);
        redissonService.addToSet("user:ids", String.valueOf(id));
        
        return Response.<String>builder()
                .code("200")
                .info("保存成功")
                .data(String.valueOf(id))
                .build();
    }

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @Override
    public Response<String> getUserToken(String id) {
        String token = (String) redissonService.getValue("user:" + id);
        if (token == null) {
            return Response.<String>builder()
                    .code("400")
                    .info("用户不存在")
                    .build();
        }
        return Response.<String>builder()
                .code("200")
                .info("获取成功")
                .data(token)
                .build();
    }
}
